package com.aaa.cts.util;

import com.aaa.common.constants.BusinessConstants;
import com.aaa.cts.entity.OperLog;
import com.aaa.cts.entity.User;
import com.aaa.cts.service.OperLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 切面功能的实现类，操作日志记录类  通知或者增强
 */
@Component
/**
 * 通知或者增强类注释
 */
@Aspect
public class OperationLogUtil {

    @Autowired
    private OperLogService operLogService;

    @Autowired
    private HttpSession session;

    /*@Pointcut(value = "execution(int *(..))")
    private void pointCut(){}*/
    /**
     * 操作日志     * @param joinPoint  后置通知
     */
    @AfterReturning(pointcut = "execution(* com.aaa.cts.service.impl.*.insert*(..))" +
            "||execution(* com.aaa.cts.service.impl.*.update*(..))" +
            "||execution(* com.aaa.cts.service.impl.*.delete*(..))")
    public void recordOperLog(JoinPoint joinPoint){
        //实例化对象
        OperLog operLog = new OperLog();
        operLog.setTitle("后台管理模块");
        //获取连接点的方法名称
        String methodName = joinPoint.getSignature().getName();
        //定义业务类型变量
        int businessType=0;
        if(methodName.contains("insert")){
            businessType= BusinessConstants.OperationLogType.INSERT;
        }else if(methodName.contains("update")){
            businessType= BusinessConstants.OperationLogType.UPDATE;
        }else {
            businessType=BusinessConstants.OperationLogType.DELETE;
        }
        //设置日志业务操作类型
        operLog.setBusinessType(businessType);
        //获取目标对象的名称
        String targetName = joinPoint.getTarget().getClass().getName();
        //操作方法名称
        operLog.setMethod(targetName+"."+methodName);
        operLog.setOperatorType(BusinessConstants.OperLogCustomerType.BACKUSER);
        User user = (User) session.getAttribute("userInfo");
        if(user!=null){
            operLog.setOperName(user.getLoginName());
            operLog.setDeptName(user.getDeptId()+"");
        }
        //获取Request对象
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request =  requestAttributes.getRequest();
        //获取ip地址
        String remoteAddr = request.getRemoteAddr();
        operLog.setOperIp(remoteAddr);
        operLog.setOperUrl(request.getRequestURI());
        //根据获取到的ip地址，调用远程接口，判断ip归属地，如果获取不到归属地，说明是内网ip
        operLog.setOperLocation("内网IP");
        //获取连接点的参数
        Object[] args = joinPoint.getArgs();
        if(args!=null && args.length>0){
            String simpleName = args[0].getClass().getSimpleName();
            operLog.setOperParam(simpleName);
        }
        operLog.setStatus(0);
        operLog.setErrorMsg("异常类名称");
        operLog.setOperTime(new Date());
        operLogService.insert(operLog);
    }

}
